package yaner.hotel.manager.resource.mapper.impl;

import org.springframework.stereotype.Component;
import yaner.hotel.manager.common.dto.role.RoleAddDto;
import yaner.hotel.manager.common.dto.role.RoleUpdateDto;
import yaner.hotel.manager.common.utils.SnowUtils;
import yaner.hotel.manager.common.vo.RoleVo;
import yaner.hotel.manager.resource.entity.Role;
import yaner.hotel.manager.resource.mapper.IRoleMapper;

import java.util.Date;
import java.util.Objects;

@Component
public class RoleMapperImpl implements IRoleMapper {
    @Override
    public RoleVo toVo(Role role) {
        if (Objects.isNull(role)) {
            return null;
        }

        return RoleVo.builder()
                .roleId(role.getRoleId())
                .roleCode(role.getRoleCode())
                .roleName(role.getRoleName())
                .roleDesc(role.getRoleDesc())
                .build();
    }


    @Override
    public Role toAddEntity(RoleAddDto roleAddDto) {
        if (Objects.isNull(roleAddDto)) {
            return null;
        }
        return Role.builder()
                .roleId(SnowUtils.nextId())
                .roleCode(roleAddDto.getRoleCode())
                .roleName(roleAddDto.getRoleName())
                .createTime(new Date())
                .updateTime(new Date())
                .roleDesc(roleAddDto.getRoleDesc())
                .build();
    }

    @Override
    public Role toUpdateEntity(RoleUpdateDto roleUpdateDto) {
        if (Objects.isNull(roleUpdateDto)) {
            return null;
        }
        return Role.builder()
                .roleCode(roleUpdateDto.getRoleCode())
                .roleName(roleUpdateDto.getRoleName())
                .roleDesc(roleUpdateDto.getRoleDesc())
                .updateTime(new Date())
                .build();
    }
}
